﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.Gateway;

namespace HRM.DAL.DAO
{
    public class Company : DBGateway
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }

        public bool SaveCompany(Company company)
        {
            if (!AllreadyHasCompany())
            {
                try
                {
                    ConnectionObj.Open();
                    CommandObj.CommandText = string.Format("insert into tblCompany values(@name,@address)");
                    CommandObj.Parameters.Clear();
                    CommandObj.Parameters.Add("@name", SqlDbType.VarChar).Value = company.Name;
                    CommandObj.Parameters.Add("@address", SqlDbType.VarChar).Value = company.Address;
                    CommandObj.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    throw new Exception("Error occured while Saving Company Info, Try again !");
                }
                finally
                {
                    ConnectionObj.Close();
                }
            }
            else
            {
                return UpdateCompanyInfo(company);
            }

        }

        private bool UpdateCompanyInfo(Company company)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("update tblCompany set name=@name,address=@address");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@name", SqlDbType.VarChar).Value = company.Name;
                CommandObj.Parameters.Add("@address", SqlDbType.VarChar).Value = company.Address;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error occured while Saving Company Info, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        private bool AllreadyHasCompany()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblCompany";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public Company GetCompanyInfo()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblCompany";
                SqlDataReader reader = CommandObj.ExecuteReader();
                Company company = new Company();
                while (reader.Read())
                {
                    company.Name = reader[1].ToString();
                    company.Address = reader[2].ToString();
                }
                return company;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}